package com.mall.system.controller;

import com.mall.common.entity.gateway.GatewayLog;
import com.mall.common.result.Result;
import com.mall.system.convert.GatewayLogConverter;
import com.mall.system.entity.SysLog;
import com.mall.system.service.SysLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 日志控制器
 */
@Slf4j
@Tag(name = "日志管理")
@RestController
@RequestMapping("/system/logs")
public class LogController {
    
    @Autowired
    private SysLogService sysLogService;
    
    @Operation(summary = "保存网关日志")
    @PostMapping("/gateway")
    public Result<Boolean> saveGatewayLog(@RequestBody GatewayLog gatewayLog) {
        try {
            SysLog sysLog = GatewayLogConverter.convertToSysLog(gatewayLog);
            boolean result = sysLogService.saveLog(sysLog);
            if (result) {
                log.info("保存网关日志成功: {}", gatewayLog.getId());
                return Result.success(true);
            } else {
                log.error("保存网关日志失败: {}", gatewayLog.getId());
                return Result.failed("保存日志失败");
            }
        } catch (Exception e) {
            log.error("保存网关日志异常: ", e);
            return Result.failed("保存日志异常");
        }
    }
}